<?php

class TaskController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        session_write_close();
        $model = new Task('search');
        if (isset($_GET['Task'])) {
            $model->attributes = $_GET['Task'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }


    public function actionDelete()
    {
        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $id = $this->getQuery('get', 'id', null);
            $model = Task::model()->with('TopicExt')->findByPk($id);
            if ($model) {
                if ($model->delete()) {
                    $result = 'Success';
                } else {
                    $result = 'delete failure';
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

    }

    public function actionCreate()
    {
        $model = new Task();
        if (isset($_POST['Task'])) {
            $model->attributes = $_POST['Task'];
            if ($model->save()) {
                Yii::app()->user->setFlash('alert-success', 'Create Success');
                $this->redirect($this->defaultAction);
            } else {
                Yii::app()->user->setFlash('alert-failure', 'Create failed');
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }


    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Task::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Task']) && ($model->attributes = $_POST['Task'])) {
                    if ($model->update()) {
                        Yii::app()->user->setFlash('alert-success', 'Update Success');
                        $this->redirect(array($this->defaultAction));
                    } else {
                        if ($model->hasErrors('status')) {
                            Yii::app()->user->setFlash('alert-failure', 'Update failure , the config can\'t work  and  error is ' . $model->getError('status'));
                        }
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
                return;
            }
        }
        Yii::app()->user->setFlash('alert-failure', "Update failure");
        $this->redirect(array($this->defaultAction));
    }

    public function actionSwitchpick()
    {
//        $id = $this->getQuery('get', 'id', null);
//        $model = MsggConfig::model()->findByPk($id);
//        if ($model) {
//            $model->status = $model->isOpen() ? MsggConfig::STATUS_CLOSE : MsggConfig::STATUS_OPEN;
//            if ($model->save()) {
//                if ($model->isClose() && !MsggConfig::model()->count('type=:type and status=:status', [':type' => $model->type, ':status' => MsggConfig::STATUS_OPEN])) {
//                    Yii::app()->user->setFlash('alert-success', "Switch Success Notice you have closed the last open channel in  {$model->type}");
//                } else {
//                    Yii::app()->user->setFlash('alert-success', "Switch Success");
//                }
//            } else {
//                Yii::app()->user->setFlash('alert-failure', 'Switch failure');
//            }
//        } else {
//            Yii::app()->user->setFlash('alert-failure', 'Switch failure');
//        }
//        $this->redirect($this->defaultAction);
    }

    public function actionLink()
    {
        if (isset($_GET['id'])) {
            $model = Task::model()->findByPk($_GET['id']);
            if (isset($_POST['Task']['sids']) && ($sids = $_POST['Task']['sids'])) {
                $model->setSids($_POST['Task']['sids']);
                if ($model->updatSeries()) {
                    Yii::app()->user->setFlash('alert-success', 'Link Success');
                    $this->redirect(array($this->defaultAction));
                } else {
                    if ($model->hasErrors('status')) {
                        Yii::app()->user->setFlash('alert-failure', 'Link failure , the config can\'t work  and  error is ' . $model->getError('status'));
                    }
                }
            }
            $this->render('link', array(
                'model' => $model,
            ));
            return;
        }
        Yii::app()->user->setFlash('alert-failure', "Link failure");
        $this->redirect(array($this->defaultAction));
    }


    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'infos':
                $duration_map = Task::getOptions('update_duration');
                $html = '';
                $html .= '<div><b>采集地址</b> ' . $data->url . '</div>';
                $html .= '<div><b>采集开始时间</b> ' . date('Y:m:d H:i:s ', $data->time_begin) . '</div>';
                $html .= '<div><b>更新周期</b> ' .$duration_map[$data->update_duration] . '</div>';
                $html .= '<div><b>是否继续</b> ' . $data->is_continue . '</div>';
                return $html;
                break;
            case 'status':
                return Task::getOptions('status')[$data->status];
                break;
            case 'time_create':
                $html = '';
                $html .= date('Y:m:d H:i:s ', $data->time_create);
                return $html;
                break;
            case 'operations':
                $params = [];
                $res = '';
                //edit
                $params[] = Yii::app()->getController()->createUrl("update", array("id" => $data->id,));
                $res .= '<a class ="" title="编辑" href="%s"> 编辑</a>';
                //delete
                $params[] = '删除';
                $params[] = Yii::app()->getController()->createUrl("delete", array("id" => $data->id,));
                $res .= '<a class ="delete "  tooltip="delete" title="%s"  href="%s"> 删除</a>';
                //$params[] = Yii::app()->getController()->createUrl("link", array("id" => $data->id,));
                // $res .= '<a class="" aria-hidden="true" href="%s">关联</a>';
                return vsprintf($res, $params);
                return vsprintf($res, $params);
                break;
            default:
                return $data->$name;
        }
    }


}